<template>
  <!-- 处置场所信息 -->
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="120px"
    >
      <el-form-item label="处置点名称" prop="solName">
        <el-input
          v-model="queryParams.solName"
          placeholder="请输入处置点名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="负责人" prop="manager">
        <el-input
          v-model="queryParams.manager"
          placeholder="请输入负责人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属省" prop="provinceCode">
        <el-select
          v-model="queryParams.provinceCode"
          clearable
          placeholder="请选择所属省"
          @change="provinceChange"
        >
          <el-option
            v-for="(v,index) in provinceList"
            :key="index"
            :label="v.provinceName"
            :value="v.provinceCode"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="所属市" prop="cityCode">
        <el-select
          v-model="queryParams.cityCode"
          clearable
          placeholder="请选择所属市"
          @change="cityChange"
        >
          <el-option
            v-for="(v,index) in cityList"
            :key="index"
            :label="v.cityName"
            :value="v.cityCode"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="所属县区" prop="areaCode">
        <el-select v-model="queryParams.areaCode" clearable placeholder="请选择区域">
          <el-option
            v-for="(v,index) in areaList"
            :key="index"
            :label="v.areaName"
            :value="v.areaCode"
          ></el-option>
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="管辖区" prop="area">
        <el-input
          v-model="queryParams.area"
          placeholder="请输入管辖区"
          clearable
          @keyup.enter.native="handleQuery"
      />-->

      <el-form-item v-if="false" label="详细地址" prop="address">
        <el-input
          v-model="queryParams.address"
          placeholder="请输入详细地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['soil:site:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['soil:site:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['soil:site:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['soil:site:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      border
      fit
      highlight-current-row
      empty-text="暂无数据"
      v-loading="loading"
      :data="soilList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />

      <!-- <el-table-column label="处置场所名称" align="center" prop="name" /> -->
      <el-table-column label="处置场所名称" align="center" prop="soilName" width="180" />
      <!-- <el-table-column label="监控视频" align="center" prop="name">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="selectJianKong(scope.row)">查看监控</el-button>
        </template>
      </el-table-column> -->

      <el-table-column label="所属省" align="center" prop="provinceName" width="80" />
      <el-table-column label="所属市" align="center" prop="cityName" width="80" />
      <el-table-column label="所属县区" align="center" prop="areaName" width="80" />
      <el-table-column label="详细地址" align="center" prop="address" width="250" />

      <el-table-column label="处置场所负责人" align="center" prop="manager" width="180"  />
      <el-table-column label="负责人联系电话" align="center" prop="managerPhone" width="180" />

      <el-table-column label="处置场所类型" align="center" prop="category" width="180" >
        <template slot-scope="scope">{{ getLabel(scope.row.category, categoryList) }}</template>
      </el-table-column>
      <el-table-column label="有效期开始日期" align="center" prop="startDate" width="150" />
      <el-table-column label="有效期结束日期" align="center" prop="endDate" width="150" />

      <el-table-column label="备注" align="center" prop="remark" width="180" />

      <!-- <el-table-column label="管辖区" align="center" prop="area" />

      <el-table-column label="详细地址" align="center" prop="address" />
      <el-table-column label="有效期开始日期" align="center" prop="startDate" />
      <el-table-column label="有效期结束日期" align="center" prop="endDate" />


      <el-table-column label="消纳渣土类型" align="center" prop="type">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.disposal_type" :value="scope.row.type"/>
        </template>
      </el-table-column>



      <el-table-column label="土场容量(方)" align="center" prop="capacity" />
      <el-table-column label="土场简称" align="center" prop="shortName" />
      <el-table-column label="土场负责人" align="center" prop="manager" />
      <el-table-column label="负责人联系电话" align="center" prop="managerPhone" />

      <el-table-column label="所属企业名称" align="center" prop="enterpriseName" />-->

      <el-table-column
        label="操作"
        fixed="right"
        align="center"
        class-name="small-padding fixed-width"
        width="180"
      >
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="generateQRCode(scope.row)">二维码</el-button>

          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['soil:site:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['soil:site:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 查看二维码 -->
    <el-dialog title="查看二维码" :visible.sync="qeCodeOpen" width="500px" append-to-body>
      <img
        :src="codeUrl"
        class="login-code-img center-image"
        style="width: 400px; min-height: 350px;"
      />
      <div slot="footer" class="dialog-footer">
        <a :href="codeUrl" download="qrcode.png">
          <el-button icon="el-icon-download" plain>下载二维码</el-button>
        </a>
        <el-button @click="cancelqeCode">取消</el-button>
      </div>
    </el-dialog>
    <!-- 添加或修改处置场所信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1200px" append-to-body>
      <dialogCSXX ref="refCSXX" :form="form" @submitForm="submitForm" @cancel="cancel" />
    </el-dialog>
  </div>
</template>

<script>
import {
  listSoil,
  getSoil,
  delSoil,
  addSoil,
  updateSoil
} from "@/api/modules/soil";
import { provinceList, cityList, areaList } from "@/api/region/region";
import { generateQRCode } from "@/api/util/qrcode";

import dialogCSXX from "./components/index";

export default {
  name: "Soil",
  components: { dialogCSXX },
  dicts: ["disposal_type"],
  data() {
    return {
      codeUrl: null,
      qeCodeOpen: false,
      provinceList: [],
      cityList: [],
      areaList: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 处置场所信息表格数据
      soilList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        area: null,
        name: null,
        address: null,
        startDate: null,
        endDate: null,
        type: null,
        capacity: null,
        shortName: null,
        regionId: null,
        manager: null,
        managerPhone: null,
        category: null,
        supervisingDepartment: null,
        organization: null,
        siteNumber: null,
        siteArea: null,
        dailyProcessingCapacity: null,
        unloadingFeePrice: null,
        attentionLevel: null,
        flushingEquipment: null,
        hardeningEquipment: null,
        isolationFence: null,
        teamId: null,
        enterpriseName: null,
        share: null,
        exts: null,
        provinceCode: null,
        provinceName: null,
        cityCode: null,
        cityName: null,
        areaCode: null,
        areaName: null,
        townName: null,
        townCode: null,
        auditFlowStatus: null,
        keycode: null,
        soilUserId: null,
        userName: null,
        surveyInformationUserId: null,
        status: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        area: [{ required: true, message: "管辖区不能为空", trigger: "blur" }],
        name: [
          { required: true, message: "处置点名称不能为空", trigger: "blur" }
        ],
        address: [
          { required: true, message: "详细地址不能为空", trigger: "blur" }
        ],
        startDate: [
          { required: true, message: "有效期开始日期不能为空", trigger: "blur" }
        ],
        endDate: [
          { required: true, message: "有效期结束日期不能为空", trigger: "blur" }
        ],
        type: [
          {
            required: true,
            message:
              "消纳渣土类型(选择：1.工程回填、2.市政建设、3.宕口修复垦、4.绿化用途、5.塌陷地回填、6.低洼地回填、7.消纳场)不能为空",
            trigger: "change"
          }
        ],
        regionId: [
          { required: true, message: "电子围栏不能为空", trigger: "blur" }
        ]
      },

      categoryList: [
        { value: "1", label: "砂石土场" },
        { value: "2", label: "土方土场" },
        { value: "3", label: "建筑垃圾土场" },
        { value: "4", label: "渣土土场" },
        { value: "5", label: "灰浆等流体土场" },
        { value: "6", label: "其他土场" }
      ]
    };
  },
  created() {
    this.getList();
    this.initProvinceList();
    this.initCityList(this.queryParams.provinceCode);
  },
  methods: {
    //显示二维码
    generateQRCode(row) {
      // 发送请求获取二维码图片
      generateQRCode(row.id)
        .then(response => {
          this.codeUrl = "data:image/gif;base64," + response.img;
          this.qeCodeOpen = true;
        })
        .catch(error => {
          console.error("Error:", error);
          // 处理错误情况
        });
    },

    initProvinceList() {
      provinceList().then(response => {
        this.provinceList = response.data;
      });
    },
    initCityList(v) {
      let obj = {
        provinceCode: v
      };
      cityList(obj).then(response => {
        this.cityList = response.data;
      });
    },
    initAreaList(v) {
      let obj = {
        cityCode: v
      };
      areaList(obj).then(response => {
        this.areaList = response.data;
      });
    },
    //所属省份选择的
    provinceChange(v) {
      this.initCityList(v);
      this.queryParams.cityCode = null;
      this.queryParams.areaCode = null;
    },
    cityChange(v) {
      this.initAreaList(v);
      this.queryParams.areaCode = null;
    },
    /** 查询处置场所信息列表 */
    getList() {
      this.loading = true;
      listSoil(this.queryParams).then(response => {
        console.log(response);

        this.soilList = response.data;
        this.total = response.total;
        this.total = 0;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    cancelqeCode() {
      this.qeCodeOpen = false;
      this.codeUrl = null;
    },
    // 查看监控
    selectJianKong(id) {
      // 新窗口的URL
      const url = "http://4g.sjzhlkj.cn:9999/shipin.html";
      // 新窗口的特征，例如宽度和高度
      const windowFeatures = "width=600,height=400";

      // 打开新窗口
      window.open(url, "_blank");
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        area: null,
        name: null,
        address: null,
        startDate: null,
        endDate: null,
        type: null,
        capacity: null,
        shortName: null,
        regionId: null,
        manager: null,
        managerPhone: null,
        category: null,
        supervisingDepartment: null,
        organization: null,
        siteNumber: null,
        siteArea: null,
        dailyProcessingCapacity: null,
        unloadingFeePrice: null,
        attentionLevel: null,
        remark: null,
        flushingEquipment: null,
        hardeningEquipment: null,
        isolationFence: null,
        teamId: null,
        enterpriseName: null,
        share: null,
        exts: null,
        provinceCode: null,
        provinceName: null,
        cityCode: null,
        cityName: null,
        areaCode: null,
        areaName: null,
        townName: null,
        townCode: null,
        auditFlowStatus: null,
        keycode: null,
        soilUserId: null,
        userName: null,
        surveyInformationUserId: null,
        status: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加处置场所信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getSoil(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改处置场所信息";

        console.log(this.$refs["refCSXX"], "refCSXX");
        if (this.$refs["refCSXX"]) {
          if (this.form.provinceCode) {
            this.$refs["refCSXX"].initCityList(this.form.provinceCode);
          }
          if (this.form.cityCode) {
            this.$refs["refCSXX"].initAreaList(this.form.cityCode);
          }
        }
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.open = false;
      this.getList();
      return;

      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateSoil(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSoil(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除处置场所信息编号为"' + ids + '"的数据项？')
        .then(function() {
          return delSoil(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "modules/soil/export",
        {
          ...this.queryParams
        },
        `soil_${new Date().getTime()}.xlsx`
      );
    },

    /** 键值对应 */
    getLabel(code, listData) {
      // console.log(code, listData )
      let labelV = code;
      for (let i = 0; i < listData.length; i++) {
        console.log(code, listData[i]);
        if (code == listData[i].value) {
          labelV = listData[i].label;
        }
      }
      console.log(labelV);
      return labelV;
    }
  }
};
</script>
